import re

java_type_mapping = {
    "TINYINT": "Integer",
    "INT": "Integer",
    "NUMBER": "Integer",
    "VARCHAR": "String",
    "NVARCHAR2": "String",
    "CHAR": "String",
    "DATE": "LocalDateTime",
    "TIMESTAMP": "LocalDateTime",
    "TEXT": "String",
    "LONGTEXT": "String",
}

sql_type_mapping = {
    'mysql': {
        "TINYINT": "tinyint",
        "INT": "decimal",
        "NUMBER": "decimal",
        "VARCHAR": "varchar",
        "NVARCHAR2": "varchar",
        "CHAR": "char",
        "DATE": "datetime",
        "TIMESTAMP": "timestamp",
        "TEXT": "text",
        "LONGTEXT": "longtext",
    }
}

sql_type_re_mapping = {
    'mysql': {
        "TINYINT": "tinyint",
        "INT": "decimal",
        "NUMBER": "decimal",
        "VARCHAR": "varchar",
        "NVARCHAR2": "varchar",
        "CHAR": "char",
        "DATE": "datetime",
        "TIMESTAMP": "timestamp",
        "TEXT": "text",
        "LONGTEXT": "longtext",
    }
}

def get_java_type(type: str):
    """
    根据Sql类型获取Java类型
    type SQL类型
    """
    if re.match('TINYINT', type.upper()):
        return java_type_mapping['TINYINT']
    elif re.match('INT', type.upper()):
        return java_type_mapping['INT']
    elif re.match('(NUMBER\(\d+\)|NUMBER\(\d+,\d+\))', type.upper()):
        return java_type_mapping['NUMBER']
    elif re.match('VARCHAR\(\d+\)', type.upper()):
        return java_type_mapping['VARCHAR']
    elif re.match('NVARCHAR2\(\d+\)', type.upper()):
        return java_type_mapping['NVARCHAR2']
    elif re.match('CHAR\(\d+\)', type.upper()):
        return java_type_mapping['CHAR']
    elif re.match('DATE', type.upper()):
        return java_type_mapping['DATE']
    elif re.match('TIMESTAMP', type.upper()):
        return java_type_mapping['TIMESTAMP']
    elif re.match('TEXT', type.upper()):
        return java_type_mapping['TEXT']
    elif re.match('LONGTEXT', type.upper()):
        return java_type_mapping['LONGTEXT']
    else:
        raise TypeError(f"""非法的数据库字段类型:{type}""")

def get_sql_type(type: str, dialect: str):
    """
    根据Sql类型获取指定方言的Sql类型
    """
    _mapping = sql_type_mapping[dialect]
    if re.match('TINYINT', type.upper()):
        return _mapping['TINYINT']
    elif re.match('INT', type.upper()):
        return type.replace('INT', _mapping['INT'])
    elif re.match('(NUMBER\(\d+\)|NUMBER\(\d+,\d+\))', type.upper()):
        return type.replace('NUMBER', _mapping['NUMBER'])
    elif re.match('VARCHAR\(\d+\)', type.upper()):
        return type.replace('VARCHAR', _mapping['VARCHAR'])
    elif re.match('NVARCHAR2\(\d+\)', type.upper()):
        return type.replace('NVARCHAR2', _mapping['NVARCHAR2'])
    elif re.match('CHAR\(\d+\)', type.upper()):
        return type.replace('CHAR', _mapping['CHAR'])
    elif re.match('DATE', type.upper()):
        return type.replace('DATE', _mapping['DATE'])
    elif re.match('TIMESTAMP', type.upper()):
        return type.replace('TIMESTAMP', _mapping['TIMESTAMP'])
    elif re.match('TEXT', type.upper()):
        return type.replace('TEXT', _mapping['TEXT'])
    elif re.match('LONGTEXT', type.upper()):
        return type.replace('LONGTEXT', _mapping['LONGTEXT'])
    else:
        raise TypeError(f"""非法的数据库字段类型:{type}""")
